package com.eedi.framework.sensitiveword.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.sensitiveword.controller.sys.vo.SysSensitiveWordPageReq;
import com.eedi.framework.sensitiveword.controller.sys.vo.SysSensitiveWordResp;
import com.eedi.framework.sensitiveword.controller.sys.vo.SysSensitiveWordSave;
import com.eedi.framework.sensitiveword.service.SysSensitiveWordService;
import com.eedi.framework.sensitiveword.dal.dataobject.SysSensitiveWordDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 敏感词")
@RestController
@RequestMapping("/sensitive-word")
@Validated
public class SysSensitiveWordController {

    @Resource
    private SysSensitiveWordService sysSensitiveWordService;

    @PostMapping("/create")
    @Operation(summary = "创建敏感词")
    @PreAuthorize("@ss.hasPermission('sys:sensitive-word:create')")
    public CommonResult<String> createSensitiveWord(@Valid @RequestBody SysSensitiveWordSave createReqVO) {
        return success(sysSensitiveWordService.createSensitiveWord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新敏感词")
    @PreAuthorize("@ss.hasPermission('sys:sensitive-word:update')")
    public CommonResult<Boolean> updateSensitiveWord(@Valid @RequestBody SysSensitiveWordSave updateReqVO) {
        sysSensitiveWordService.updateSensitiveWord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除敏感词")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:sensitive-word:delete')")
    public CommonResult<Boolean> deleteSensitiveWord(@RequestParam("sysSensitiveWordId") String sysSensitiveWordId) {
        sysSensitiveWordService.deleteSensitiveWord(sysSensitiveWordId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得敏感词")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:sensitive-word:query')")
    public CommonResult<SysSensitiveWordResp> getSensitiveWord(@RequestParam("sysSensitiveWordId") String sysSensitiveWordId) {
        SysSensitiveWordDO sensitiveWord = sysSensitiveWordService.getSensitiveWord(sysSensitiveWordId);
        return success(BeanUtils.toBean(sensitiveWord, SysSensitiveWordResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得敏感词分页")
    @PreAuthorize("@ss.hasPermission('sys:sensitive-word:query')")
    public CommonResult<PageResult<SysSensitiveWordResp>> getSensitiveWordPage(@Valid SysSensitiveWordPageReq pageVO) {
        PageResult<SysSensitiveWordDO> pageResult = sysSensitiveWordService.getSensitiveWordPage(pageVO);
        return success(BeanUtils.toBean(pageResult, SysSensitiveWordResp.class));
    }


    @GetMapping("/get-tags")
    @Operation(summary = "获取所有敏感词的标签数组")
    @PreAuthorize("@ss.hasPermission('sys:sensitive-word:query')")
    public CommonResult<Set<String>> getSensitiveWordTagSet() {
        return success(sysSensitiveWordService.getSensitiveWordTagSet());
    }

    @GetMapping("/validate-text")
    @Operation(summary = "获得文本所包含的不合法的敏感词数组")
    public CommonResult<List<String>> validateText(@RequestParam("text") String text,
                                                   @RequestParam(value = "tags", required = false) List<String> tags) {
        return success(sysSensitiveWordService.validateText(text, tags));
    }

}
